﻿using QuickApp;
using QuickApp.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;

namespace QA.Wpf
{
    public class QATreeView : TreeView
    {
        #region Public Fields

        public static readonly DependencyProperty QAParentProperty = DependencyProperty.Register("QAParent", typeof(IQAParent), typeof(QATreeView), new PropertyMetadata(new PropertyChangedCallback(qAParentChanged)));
        public static readonly DependencyProperty SelectedObjectProperty = DependencyProperty.Register("SelectedObject", typeof(object), typeof(QATreeView));

        #endregion Public Fields

        #region Public Properties

        /// <summary>
        /// This parent will not be actually added to the tree, it will add all the children into it
        /// </summary>
        public IQAParent QAParent
        {
            get
            {
                return (IQAParent)this.GetValue(QATreeView.QAParentProperty);
            }
            set
            {
                this.SetValue(QATreeView.QAParentProperty, value);
            }
        }

        public object SelectedObject
        {
            get
            {
                return (object)this.GetValue(QATreeView.SelectedObjectProperty);
            }
            set
            {
                this.SetValue(QATreeView.SelectedObjectProperty, value);
            }
        }

        #endregion Public Properties

        #region Protected Methods

        protected override void OnSelectedItemChanged(RoutedPropertyChangedEventArgs<object> e)
        {
            QATreeViewItem item = e.Source as QATreeViewItem;
            if (item != null)
            {
                this.SelectedObject = item.CurrentObject;
            }
            base.OnSelectedItemChanged(e);
        }

        #endregion Protected Methods

        #region OnDependencyPropertyChanged

        private static void qAParentChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
        {
            (obj as QATreeView).buildList(args.NewValue as IQAParent);
        }

        #endregion OnDependencyPropertyChanged

        #region Private Methods
        
        private void buildList(IQAParent qaParent)
        {
            if (qaParent != null && !qaParent.Children.IsNullOrEmpty())
            {
                ItemsControl toAddChildren = this;
                foreach (KeyValuePair<string, List<IQAChild>> kv in qaParent.Children)
                {
                    if (qaParent.Children.Count > 1)
                    {
                        toAddChildren = new QATreeViewItem() { Header = kv.Key, QATreeView = this};
                        this.Items.Add(toAddChildren);
                    }
                    foreach (IQAChild child in kv.Value)
                    {
                        toAddChildren.Items.Add(new QATreeViewItem()
                        {
                            Header = "suckit",//child.DisplayName,
                            CurrentObject = child,
                            QATreeView = this
                        });
                    }
                }
            }
        }

        #endregion Private Methods
    }
}
